# -*- coding: utf-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：encryption.py
#日期：2019-07-31
#备注：多种加解密方法    # pip install pycryptodome
用pyCryptodome模块带的aes先将秘钥以及要加密的文本填充为16位   AES key must be either 16 or 32 bytes long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import base64
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from pycacho.cachobase.file_deal import save_file

count = 16


def generate_key(private_file,public_file):
    key = RSA.generate(2048)  # 256倍数并且>= 1024
    # 提取私钥并存入文件
    private_key = key.exportKey()
    save_file(private_file,private_key)

    # 提取公钥存入文件
    public_key = key.publickey().exportKey()
    save_file(public_file,public_key)

def aes_encrypt_ecb(org_str, key):
    #print('--------------->电码本模式ECB加密开始<---------------')
    aes = AES.new(get_bytes(key), AES.MODE_ECB)
    encrypt_aes = aes.encrypt(get_bytes(org_str))
    encrypted_text = str(base64.b64encode(encrypt_aes), encoding='utf-8')
    print('加密后的数据：', encrypted_text)
    return (encrypted_text)


def aes_decrypt_ecb(secret_str, key):
    #print('--------------->电码本模式ECB解密开始<---------------')
    aes = AES.new(get_bytes(key), AES.MODE_ECB)
    base64_decrypted = base64.b64decode(secret_str.encode(encoding='utf-8'))
    decrypted_text = str(aes.decrypt(base64_decrypted), encoding='utf-8').replace('\0', '')
    print('解密后的数据：', decrypted_text,'\n')
    return(decrypted_text)


def aes_encrypt_cbc(org_str, key,iv):
    print('--------------->密码分组链接模式CBC加密开始<---------------')
    aes = AES.new(get_bytes(key), AES.MODE_CBC,get_bytes(iv))
    encrypt_aes = aes.encrypt(get_bytes(org_str))
    encrypted_text = str(base64.b64encode(encrypt_aes), encoding='utf-8')
    print('加密后的数据：', encrypted_text)
    return (encrypted_text)


def aes_decrypt_cbc(secret_str, key,iv):
    print('--------------->密码分组链接模式CBC解密开始<---------------')
    aes = AES.new(get_bytes(key), AES.MODE_CBC,get_bytes(iv))
    base64_decrypted = base64.b64decode(secret_str.encode(encoding='utf-8'))
    decrypted_text = str(aes.decrypt(base64_decrypted), encoding='utf-8').replace('\0', '')
    print('解密后的数据：', decrypted_text)


# bytes不是count的倍数那就补足为count的倍数
def add_to_count(value):
    while len(value) % count != 0:
        value += b'\x00'
    return value                            # 返回bytes


# str转换为bytes超过count位时处理
def get_bytes(org_str):
    org_bytes = str.encode(org_str)
    n = int(len(org_bytes) / count)
    i = 0
    new_bytes = b''
    while n >= 1:
        i = i + 1
        new_byte = org_bytes[(i-1)*count:count*i-1]
        new_bytes += new_byte
        n = n - 1
    if len(org_bytes) % count == 0:
        all_bytes = org_bytes
    elif len(org_bytes) % count != 0 and n>1:
        all_bytes = new_bytes + add_to_count (org_bytes[i*count:])
    else:
        all_bytes = add_to_count (org_bytes)
    print(all_bytes)
    return all_bytes


if __name__ == '__main__':
    #org_str = 'http://mp.weixin.qq.com/s?__biz=MjM5NjAxOTU4MA==&amp;mid=3009217590&amp;idx=1&amp;sn=14532c49bc8cb0817544181a10e9309f&amp;chksm=90460825a7318133e7905c02e708d5222abfea930e61b4216f15b7504e39734bcd41cfb0a26d&amp;scene=27#wechat_redirect'
    org_str = 'Iamview1234#'           # 要加密的内容
    key = 'CACHO11111111111'                # 秘钥,不能为空
    iv = '0123456789ABCDEF'         # 分组变量
    aes_encrypt_cbc(org_str, key, iv)  # 加密
    #secret_str = 'jKb/4qUeAAvHeTBWO1vr8CQ/SlTeT6qiMZLAYE+hVko='
    #aes_decrypt_cbc(secret_str, key, iv)  # 解密

